import QtQuick 1.1
import org.kde.plasma.core 0.1 as PlasmaCore
import org.kde.plasma.extras 0.1 as PlasmaExtras
import "../code/task.js" as TaskFunctions

Item {
    id: taskDelegate
    
    property string source
    property bool active
    property bool minimized
    property bool maximized
    property bool attention : false // TODO: make it work
    
    property alias hover: mouseArea.containsMouse
    property alias pressed: mouseArea.pressed
    property alias margins : taskContent.margins
    property alias label: taskContent.label
    property alias icon: taskContent.icon
    property alias showLabel: taskContent.showLabel
    property alias showControls: taskContent.showControls
    property alias orientation: taskContent.orientation

    
    visible: false
    
    signal rightClicked()
    signal leftClicked()
    signal middleClicked()

    onLeftClicked: TaskFunctions.activateRaiseOrIconify(source)

    
    MouseArea {
        id: mouseArea
        
        hoverEnabled: true
        acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidleButton
        
        anchors.fill:parent
            
        onPressed: PlasmaExtras.PressedAnimation {
            targetItem: taskDelegate
        }
        
        onReleased: PlasmaExtras.ReleasedAnimation {
            targetItem: taskDelegate
        }
        
        onClicked: {
            if(mouse.button == Qt.LeftButton)
                taskDelegate.leftClicked()
            else if(mouse.button == Qt.RightButton)
                taskDelegate.rightClicked()
            else if(mouse.button == Qt.MiddleButton)
                taskDelegate.middleClicked()
        }
    
        TaskContent {
            id: taskContent
            
            margins: normalFrame.margins
            source: taskDelegate.source
            maximized: taskDelegate.maximized
            
            anchors.fill: parent
                
            Behavior on anchors.topMargin {
                NumberAnimation {
                    alwaysRunToEnd :true
                    duration: 100 
                }
            }
            
            Behavior on anchors.bottomMargin {
                NumberAnimation {
                    alwaysRunToEnd :true
                    duration: 100
                }
            }
            
            Behavior on anchors.leftMargin {
                NumberAnimation {
                    alwaysRunToEnd :true
                duration: 100
                }
            }
            
            Behavior on anchors.rightMargin {
                NumberAnimation {
                    alwaysRunToEnd :true
                    duration: 100
                }
            }
        }
    }

    
    PlasmaCore.FrameSvgItem {
        id: normalFrame
        
        imagePath: "widgets/tasks"
        prefix: "normal"

        opacity: 0
        
        anchors.fill: parent
    }
    
    PlasmaCore.FrameSvgItem {
        id: minimizedFrame
        
        imagePath: "widgets/tasks"
        prefix: "minimized"
        
        opacity: 0
        
        anchors.fill: parent
    }
    
    PlasmaCore.FrameSvgItem {
        id: focusFrame
        
        imagePath: "widgets/tasks"
        prefix: "focus"
        
        opacity: 0
        
        anchors.fill: parent
    }
     
     PlasmaCore.FrameSvgItem {
        id: hoverFrame
        
        imagePath: "widgets/tasks"
        prefix: "hover"
        
        opacity: 0
        
        anchors.fill: parent
    }
 
    PlasmaCore.FrameSvgItem {
        id: attentionFrame
        
        imagePath: "widgets/tasks"
        prefix: "attention"
        
        opacity: 0
        
        anchors.fill: parent
    }

    states: [
        State {
            name: "PRESSED"
            when: taskDelegate.pressed
               
            PropertyChanges {
                target: taskDelegate
                    
                visible: true
                margins: attentionFrame.margins
            }

            PropertyChanges {
                target: normalFrame
                    
                opacity: 0
            }
             
            PropertyChanges {
                target: minimizedFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: focusFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: hoverFrame
                    
                opacity: 0
            }
                        
            PropertyChanges {
                target: attentionFrame
                    
                opacity: 1
            }
        }
        ,
        State {
            name: "HOVER"
            when: taskDelegate.hover
               
            PropertyChanges {
                target: taskDelegate
                    
                visible: true
                margins: hoverFrame.margins
            }

            PropertyChanges {
                target: normalFrame
                    
                opacity: 0
            }
             
            PropertyChanges {
                target: minimizedFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: focusFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: hoverFrame
                    
                opacity: 1
            }
                        
            PropertyChanges {
                target: attentionFrame
                    
                opacity: 0
            }
        } 
        ,
        State { // TODO: make it blink
            name: "ATTENTION"
            when: taskDelegate.attention
               
            PropertyChanges {
                target: taskDelegate
                    
                visible: true
                margins: focusFrame.margins
            }

            PropertyChanges {
                target: normalFrame
                    
                opacity: 0
            }
             
            PropertyChanges {
                target: minimizedFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: focusFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: hoverFrame
                    
                opacity: 0
            }
                        
            PropertyChanges {
                target: attentionFrame
                    
                opacity: 1
            }
        }
        ,
        State {
            name: "ACTIVE"
            when: taskDelegate.active
               
            PropertyChanges {
                target: taskDelegate
                    
                visible: true
                margins: focusFrame.margins
            }
            
            PropertyChanges {
                target: normalFrame
                    
                opacity: 0
            }
             
            PropertyChanges {
                target: minimizedFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: focusFrame
                    
                opacity: 1
            }
            
            PropertyChanges {
                target: hoverFrame
                    
                opacity: 0
            }
                        
            PropertyChanges {
                target: attentionFrame
                    
                opacity: 0
            }
        }
        ,
        State {
            name: "MINIMIZED"
            when: taskDelegate.minimized
               
            PropertyChanges {
                target: taskDelegate
                    
                visible: true
                margins: minimizedFrame.margins
            }

            PropertyChanges {
                target: normalFrame
                    
                opacity: 0
            }
             
            PropertyChanges {
                target: minimizedFrame
                    
                opacity: 1
            }
            
            PropertyChanges {
                target: focusFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: hoverFrame
                    
                opacity: 0
            }
                        
            PropertyChanges {
                target: attentionFrame
                    
                opacity: 0
            }
        } 
        ,
        State {
            name: "NORMAL"
            when: !taskDelegate.minimized

            PropertyChanges {
                target: taskDelegate
                    
                visible: true
                margins: normalFrame.margins
            }
            
            PropertyChanges {
                target: normalFrame
                    
                opacity: 1
            }
             
            PropertyChanges {
                target: minimizedFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: focusFrame
                    
                opacity: 0
            }
            
            PropertyChanges {
                target: hoverFrame
                    
                opacity: 0
            }
                        
            PropertyChanges {
                target: attentionFrame
                    
                opacity: 0
            }
        } 
    ]
    
    transitions: [
        Transition {
            from: ""
            
            PlasmaExtras.AppearAnimation {
                targetItem: taskDelegate
            }
        }
        ,
        Transition {
            to: ""
            
            PlasmaExtras.DisappearAnimation {
                targetItem: taskDelegate
            }
        }
        ,
        Transition {
            ParallelAnimation {
                NumberAnimation {
                    target: normalFrame
                    property: "opacity"
                    
                    alwaysRunToEnd :true
                    duration: 100
                }
                
                NumberAnimation {
                    target: minimizedFrame
                    property: "opacity"
                    
                    alwaysRunToEnd :true
                    duration: 100
                }
                
                NumberAnimation {
                    target: focusFrame
                    property: "opacity"
                    
                    alwaysRunToEnd :true
                    duration: 100
                }
                
                NumberAnimation {
                    target: hoverFrame
                    property: "opacity"
                    
                    alwaysRunToEnd :true
                    duration: 100
                }
                
                NumberAnimation {
                    target: attentionFrame
                    property: "opacity"
                    
                    alwaysRunToEnd :true
                    duration: 100
                }
            }
        }
    ]
}